package io.litchi.common.result;

import io.litchi.common.enums.ResultCodeAndMessage;
import lombok.Data;

import javax.swing.plaf.PanelUI;
import java.util.Objects;

/**
 * description: Result
 *
 * @author helang
 * @date 2025-09-02 15:04
 */
@Data
public class Result<R> {

    private String message;

    private R data;

    private Integer code;

    private Result() {

    }

    private Result(R data) {
        this.data = data;
    }

    private Result(R data, Integer code) {
        this.data = data;
        this.code = code;
    }

    private Result(R data, Integer code, String message) {
        this.data = data;
    }

    private static <R> Result<R> instanceSuccess() {
        Result<R> result = new Result<>();
        result.setCode(ResultCodeAndMessage.SUCCESS.getCode());
        result.setMessage(ResultCodeAndMessage.SUCCESS.getMessage());
        return result;
    }

    private static <R> Result<R> instanceFailure() {
        Result<R> result = new Result<>();
        result.setCode(ResultCodeAndMessage.FAIL.getCode());
        result.setMessage(ResultCodeAndMessage.FAIL.getMessage());
        return result;
    }

    public static <R> Result<R> success() {
        return instanceSuccess();
    }


    public static <R> Result<R> success(R data) {
        Result<R> instance = instanceSuccess();
        instance.setData(data);
        return instance;
    }

    public static <R> Result<R> success(R data, Integer code) {
        Result<R> instance = instanceSuccess();
        instance.setData(data);
        instance.setCode(code);
        return instance;
    }

    public static <R> Result<R> success(R data, Integer code, String message) {
        Result<R> instance = instanceSuccess();
        instance.setData(data);
        instance.setCode(code);
        instance.setMessage(message);
        return instance;
    }

    public static <R> Result<R> fail() {
        return instanceFailure();
    }

    public static <R> Result<R> fail(String message) {
        Result<R> instance = instanceFailure();
        instance.setMessage(message);
        return instance;
    }

    public static <R> Result<R> fail(String message, Integer code) {
        Result<R> instance = instanceFailure();
        instance.setMessage(message);
        instance.setCode(code);
        return instance;
    }

    public static <R> Result<R> fail(String message, Integer code, R data) {
        Result<R> instance = instanceFailure();
        instance.setData(data);
        instance.setCode(code);
        instance.setMessage(message);
        return instance;
    }

    public Boolean isSuccess() {
        return Objects.equals(this.code, ResultCodeAndMessage.SUCCESS.getCode());
    }
    public Boolean isSuccess(Integer targetCode) {
        return Objects.equals(this.code, targetCode);
    }


}